package com.quanyan.place.web.api;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.ServletUtils;
import com.quanyan.place.biz.IBaseService;
import com.quanyan.place.service.BaseService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

/**
 * Created by yangyun on 16-5-25.
 */
@RestController
@RequestMapping("/api/place/base")
public class ApiBaseController {

    @Autowired
    BaseService baseService;

    @Autowired
    IBaseService iBaseService;

    @RequestMapping(value = "/ip" ,method = RequestMethod.GET)
    @ResponseBody
    public String ip(){
        HttpServletRequest httpServletRequest = ServletUtils.getHttpServletRequest();
        return httpServletRequest.getLocalAddr() + ":" +httpServletRequest.getLocalPort();
    }

    /**
     * 获取开通城市
     * @return
     */
    @RequestMapping(value = "/getActiveCitys")
    public APIResponse<?> getActiveCitys(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Double lng = MapUtils.getDouble(map, "lng");//116.46
        Double lat = MapUtils.getDouble(map, "lat");//39.92

        if (StringUtils.isEmpty(lng)) {
            lng = Constants.BEIJING_LNG;
        }
        if (StringUtils.isEmpty(lat)) {
            lat = Constants.BEIJING_LAT;
        }

        Integer provinceId = MapUtils.getInteger(map, "provinceId");
        return iBaseService.getCitys(provinceId,true,true,lng,lat);
    }

    /**
     * 获取开通城市(只针对场馆）
     * @return
     */
    @RequestMapping(value = "/getActiveCitysForPlace")
    public APIResponse<?> getActiveCitysForPlace(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Double lng = MapUtils.getDouble(map, "lng");//116.46
        Double lat = MapUtils.getDouble(map, "lat");//39.92

        if (StringUtils.isEmpty(lng)) {
            lng = Constants.BEIJING_LNG;
        }
        if (StringUtils.isEmpty(lat)) {
            lat = Constants.BEIJING_LAT;
        }

        Integer provinceId = MapUtils.getInteger(map, "provinceId");
        return iBaseService.getCitys(provinceId, true, false, lng, lat);
    }

    @RequestMapping(value = "/accusation")
    public APIResponse<?> accusation(@RequestBody Map<String, Object> map){

        return APIResponse.returnSuccess();
    }

    public static void main(String[] args) {
        Date currDate = new Date(System.currentTimeMillis() - 60*60*1000);
        System.out.println(currDate.toString());
    }

}
